﻿using System;
using CompactMapper.Orm.Contracts;
using CompactMapper.Orm.Extensions;

namespace CompactMapper.Orm.UnitOfWork.Mappings
{
    public class GuidKey : IKey 
    {
        public static readonly GuidKey EmptyGuidKey = new GuidKey(Guid.Empty);
        private readonly Guid _guid;


        public GuidKey(Guid guid)
        {
            _guid = guid;
        }

        private Guid Guid
        {
            get { return _guid; }
        }

        public Boolean IsSet
        {
            get { return this != EmptyGuidKey; }
        }

        //public Guid Value
        //{
        //    get { return _guid; }
        //}

        public Object Value
        {
            get { return _guid; }
        }

        public bool Equals(IKey other)
        {
            if (other == null)
                return false;

            var otherGuid = other as GuidKey;
            if (otherGuid == null)
                return false;

            if (!this.IsSet() && !otherGuid.IsSet())
                return false;

            return Guid.Equals(otherGuid.Guid);
        }

        public override bool Equals(object obj)
        {
            return Equals((IKey)obj);
        }

        public override int GetHashCode()
        {
            return _guid.GetHashCode();
        }

        public override string ToString()
        {
            var guidWithoutHyphens = _guid.ToString("N");
            return guidWithoutHyphens;
        }
    }
}